package com.zhipu.balance.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import com.zhipu.balance.remotedata.ViewModelFactory;
import com.zhipu.balance.utils.InitializeUtils;

import javax.inject.Inject;

public abstract class BaseFragment<V extends ViewDataBinding> extends Fragment {
    //
    protected Context mContext;
    //
    protected V binding;
    @Inject
    public ViewModelFactory viewModelFactory;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != getContext()) {
            mContext = getContext();
        } else if (null != getActivity()) {
            mContext = getActivity();
        } else if (null != InitializeUtils.INSTANCE.getMBaseApplication()) {
            mContext = InitializeUtils.INSTANCE.getMBaseApplication();
        } else {
            return;
        }

    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, initContentView(inflater, container, savedInstanceState), container, false);
        return binding.getRoot();
    }



    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    public abstract int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //页面数据初始化方法
        initView();
        //页面数据初始化方法
        initData();
        //页面事件监听的方法，一般用于ViewModel层转到View层的事件注册
        initViewObservable();
    }

    public void initView() {

    }

    public void initData() {

    }

    public void initViewObservable() {

    }
}
